package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.content.api.common.dto.MemberInfoDTO;
import com.ibeeking.found.content.api.feign.ContentMemberBClient;
import com.ibeeking.found.content.b.rest.service.IArticleCommentService;
import com.ibeeking.found.content.b.rest.service.IArticleService;
import com.ibeeking.found.content.service.common.dos.ArticleCommentDO;
import com.ibeeking.found.content.service.common.query.ArticleCommentPageQuery;
import com.ibeeking.found.content.service.common.query.ArticleQuery;
import com.ibeeking.found.content.service.common.vo.ArticleCommentVO;
import com.ibeeking.found.content.service.common.vo.ArticleVO;
import com.ibeeking.found.content.service.mapper.ArticleCommentMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName ArticleCommentServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-21 16:01
 **/
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleCommentDO> implements IArticleCommentService {

    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private IArticleService articleServiceImpl;
    @Resource
    private ContentMemberBClient contentMemberBClient;

    @Override
    public Page<ArticleCommentVO> pageList(ArticleCommentPageQuery articleCommentPageQuery) {
        LambdaQueryWrapper<ArticleCommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(articleCommentPageQuery.getArticleId() != null, ArticleCommentDO::getArticleId, articleCommentPageQuery.getArticleId());
        wrapper.ge(StringUtils.isNotBlank(articleCommentPageQuery.getCreateTimeStart()), ArticleCommentDO::getCreateTime, articleCommentPageQuery.getCreateTimeStart());
        wrapper.le(StringUtils.isNotBlank(articleCommentPageQuery.getCreateTimeEnd()), ArticleCommentDO::getCreateTime, articleCommentPageQuery.getCreateTimeEnd());
        wrapper.orderByDesc(ArticleCommentDO::getCreateTime);
        Page<ArticleCommentDO> pages = articleCommentMapper.selectPage(new Page<ArticleCommentDO>(articleCommentPageQuery.getPageNum(), articleCommentPageQuery.getPageSize()), wrapper);
        Page<ArticleCommentVO> articleCommentVOPage = BeanUtil.convertPage(pages, ArticleCommentVO.class);
        List<ArticleCommentVO> records = articleCommentVOPage.getRecords();
        wrapperVO(records);
        return articleCommentVOPage;
    }

    @Override
    public ArticleCommentVO view(Long id) {
        ArticleCommentDO articleCommentDO = articleCommentMapper.selectById(id);
        ArticleCommentVO articleCommentVO = JsonUtils.fromJson(articleCommentDO, ArticleCommentVO.class);
        wrapperVO(Collections3.newArrayList(articleCommentVO));
        return articleCommentVO;
    }

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        ArticleCommentDO articleCommentDO = new ArticleCommentDO();
        articleCommentDO.setId(id);
        articleCommentDO.setPublishStatus(status);
        articleCommentDO.setModifyTime(LocalDateTime.now());
        return articleCommentMapper.updateById(articleCommentDO) > 0;
    }

    public void wrapperVO(List<ArticleCommentVO> vos) {
        if (Collections3.isEmpty(vos)) {
            return;
        }
        Set<Long> articleIds = vos.stream().map(ArticleCommentVO::getArticleId).collect(Collectors.toSet());
        Set<Long> memberIds = vos.stream().map(ArticleCommentVO::getMemberId).collect(Collectors.toSet());
        List<ArticleVO> articleVOList = articleServiceImpl.list(new ArticleQuery().setIds(Collections3.newArrayList(articleIds)));
        Map<Long, String> articleMap = articleVOList.stream().collect(Collectors.toMap(ArticleVO::getId, ArticleVO::getTitle));
        ResponseResult<List<MemberInfoDTO>> responseResult = contentMemberBClient.infoList(memberIds);
        List<MemberInfoDTO> memberInfoDTOList = Optional.ofNullable(responseResult.getData()).orElse(Collections3.emptyList());
        Map<Long, String> memberMap = memberInfoDTOList.stream().collect(Collectors.toMap(MemberInfoDTO::getId, x -> x.getNickname()));
        for (ArticleCommentVO vo : vos) {
            vo.setArticleTitle(articleMap.get(vo.getArticleId()));
            vo.setMemberName(memberMap.get(vo.getMemberId()));
        }
    }
}
